import { Controller, Get, Post, Body, Patch, Param, Delete, Inject, Res, Session } from '@nestjs/common';
import { LoginService } from './login.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreateLoginDto, LoginDto } from './dto/create-login.dto';
import { Response, response } from 'express';

@Controller('')
@ApiTags('登录注册')
export class LoginController {

  @Inject()
  private readonly loginService: LoginService;

  //注册
  @Post("/register")
  @ApiOperation({
    summary: '注册',
  })
  create(@Body() createLoginDto: CreateLoginDto) {
    return this.loginService.create(createLoginDto);
  }

  //登录
  @Post('/login')
  @ApiOperation({
    summary: '登录',
  })
  async login(@Body() loginDto: LoginDto, @Res({ passthrough: true }) response: Response) {
    const state = await this.loginService.login(loginDto);
    response.setHeader('Set-Cookie', `token=${state.token}; HttpOnly; Path=/; Max-Age=3600`);
    return state
  }
}
